package tomoto.util;

/**
 * Pair of objects of the same time.
 * 
 * @author Tomoto Shimizu Washio
 * 
 * @param <T> object type
 */
public class Pair<T> {
	/** first object */
	public T car;
	/** second object */
	public T cdr;

	/**
	 * Constructs a pair having the specified objects.
	 * 
	 * @param car first object
	 * @param cdr second object
	 */
	public Pair(T car, T cdr) {
		this.car = car;
		this.cdr = cdr;
	}
	
	/**
	 * Constructs a pair having nulls.
	 */
	public Pair() {
		this(null, null);
	}

	/**
	 * {@inheritDoc} Returns true if two pairs are equal, which means cars and
	 * cdrs of both pairs are respectively equal.
	 */
	@Override
	public boolean equals(Object other) {
		if (other instanceof Pair) {
			Pair<?> o = (Pair<?>) other;
			return ObjectUtil.equals(car, o.car) && ObjectUtil.equals(cdr, o.cdr);
		} else {
			return false;
		}
	}

	/**
	 * {@inheritDoc} Returns a hash code of this pair.
	 */
	@Override
	public int hashCode() {
		return ObjectUtil.hashCode(car) * 13 + ObjectUtil.hashCode(cdr);
	}

	/**
	 * Swaps both objects.
	 */
	public void swap() {
		T tmp = car;
		car = cdr;
		cdr = tmp;
	}
}
